package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ISearchService;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-11-02
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class SearchController {

    @Reference(timeout = 3000, check = false)
    ISearchService searchService;

    @Anoymous
    @GetMapping("/search/{keyword}")
    public ResponseData searchComplete(@PathVariable("keyword") String keyword) {
        SearchCompleteRequest request = new SearchCompleteRequest();
        request.setKeyword(keyword);
        // ###########################
        SearchCompleteResponse response = searchService.searchComplete(request);
        List<String> keys = response.getData();
        List<ProductDto> productDtos = new ArrayList<>();
        keys.forEach(key -> {
            ProductDto productDto = new ProductDto();
            productDto.setProductName(key);
            productDtos.add(productDto);
        });
        // ##########################
//        SearchContentResponse response = searchService.searchComplete(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
//            return new ResponseUtil().setData(response.getProductDtos());
            return new ResponseUtil().setData(productDtos);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @Anoymous
    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchContentRequest req) {
        SearchContentResponse response = searchService.search(req);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getData());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
